package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shopping_08_20/api/request"
	"shopping_08_20/internal/model"
)

func AddComment(c *gin.Context) {
	var req request.AddCommentForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	value, _ := c.Get("user_id")

	if value != req.UserId {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "权限不足",
			"data":    "",
		})
		return
	}

	var comment model.Comment

	comment = model.Comment{
		UserId:  req.UserId,
		ShopId:  req.ShopId,
		Content: req.Content,
	}

	err := comment.AddComment()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "添加失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "添加成功",
		"data":    comment,
	})
}

func FindShopComment(c *gin.Context) {
	var req request.FindShopCommentForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var comment model.Comment

	comment.ShopId = req.ShopId

	finComment, err := comment.ShopFinComment()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "查看失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "查看成功",
		"data":    finComment,
	})
}

func UpdateComment(c *gin.Context) {
	var req request.UpdateCommentForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	value, _ := c.Get("user_id")

	if value != req.UserId {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "权限不足",
			"data":    "",
		})
		return
	}

	var comment model.Comment

	comment = model.Comment{
		Id:      req.Id,
		Content: req.Content,
	}

	err := comment.UpdateComment()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "修改失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "修改成功",
		"data":    comment,
	})
}

func DeleteComment(c *gin.Context) {
	var req request.DeleteCommentForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var comment model.Comment
	comment.Id = req.Id

	err := comment.IdGetComment()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "获取失败",
			"data":    err.Error(),
		})
		return
	}

	value, _ := c.Get("user_id")

	if value != comment.UserId {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "权限不足",
			"data":    "",
		})
		return
	}

	err = comment.DeleteComment()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    100000,
			"message": "删除失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "删除成功",
		"data":    "",
	})
}
